﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2009-07-19 13:44:26Z</CreationDate>
</File>
*/
#endregion


using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;

namespace Outcoder.UI.Xaml
{
	/* TODO: [DV] Comment. */
	public static class UIElementExtensionsForSilverlight
	{
		public static CompositeTransform GetCompositeTransform(this UIElement element)
		{
			ArgumentValidator.AssertNotNull(element, "element");

			if (element.RenderTransform != null)
			{
				var compositeTransform = element.RenderTransform as CompositeTransform;
				if (compositeTransform != null)
				{
					return compositeTransform;
				}

				var transformGroup = element.RenderTransform as TransformGroup;
				if (transformGroup != null)
				{
					return transformGroup.Children.OfType<CompositeTransform>().FirstOrDefault();
				}
			}
			return null;
		}

		public static Point GetTranslatePoint(this UIElement element)
		{
			ArgumentValidator.AssertNotNull(element, "element");

			CompositeTransform transform = element.GetCompositeTransform();
			if (transform == null)
			{
				throw new ArgumentException("The specified UIElement does not have CompositeTranform.", "element");
			}

			return new Point(
				(double)transform.GetValue(CompositeTransform.TranslateXProperty),
				(double)transform.GetValue(CompositeTransform.TranslateYProperty));
		}

		public static void SetTranslatePoint(this UIElement element, Point point)
		{
			ArgumentValidator.AssertNotNull(element, "element");

			CompositeTransform transform = element.GetCompositeTransform();
			if (transform == null)
			{
				throw new ArgumentException("The specified UIElement does not have CompositeTranform.", "element");
			}

			transform.SetValue(CompositeTransform.TranslateXProperty, point.X);
			transform.SetValue(CompositeTransform.TranslateYProperty, point.Y);
		}

		public static void SetTranslatePoint(this UIElement element, double x, double y)
		{
			ArgumentValidator.AssertNotNull(element, "element");
			element.SetTranslatePoint(new Point(x, y));
		}
	}
}
